#!/usr/bin/env php
<?php
function main($argv)
{/*{{{*/
    $conn=mysqli_init();
    $conn->options(MYSQLI_INIT_COMMAND, "set names utf8");
    if(isset($argv[1]))
        $host=$argv[1];
    else
        $host='localhost';
    if($conn->real_connect($host, 'dbuser', 'dbuser', 'test',
                0, null, MYSQLI_CLIENT_FOUND_ROWS) == false)
    {
        printf("connect to db fail:%d\n", mysqli_connect_errno());
        return 1;
    }

    $stmt=$conn->prepare(
            "select empno,date_format(update_ts,'%Y%m%d%H%i%s'),name ".
            "from my_emp where empno<?"
            );
    if($stmt === false)
    {
        printf("prepare fail: %d %s\n", $conn->errno, $conn->error);
        return 1;
    }
    $empno=100;
    $stmt->bind_param('i', $empno);
    if($stmt->execute() == false)
    {
        printf("execute fail: %d %s\n", $stmt->errno, $stmt->error);
        return 1;
    }
    $stmt->store_result();
    printf("num: %d\n", $stmt->num_rows);
    $stmt->bind_result($empno, $update_ts, $name);
    while($stmt->fetch())
    {
        printf("%d %s %s\n", $empno, $update_ts, $name);
    }
    $stmt->close();
    print "----------------------------------------\n";

    $stmt=$conn->prepare(
            "update my_emp set update_ts=sysdate(),age=? ".
            "where empno=?"
            );
    $age=30;
    $empno=1;
    $stmt->bind_param('ii', $age, $empno);
    if($stmt->execute() == false)
    {
        printf("update fail: %d %s\n", $stmt->errno, $stmt->error);
        return 1;
    }
    printf("update errno: %d %s\n", $stmt->errno, $stmt->error);
    if($stmt->affected_rows == 0)
        printf("update no row\n");
    else
        printf("update succeed\n");
    $stmt->close();
    print "----------------------------------------\n";

    $conn->close();

    return 0;
}/*}}}*/

main($argv);
?>
